package com.cmict.data.infra.dal.mysql.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.infra.enums.message.UserAnnouncementStateEnum;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementDTO;
import com.cmict.data.infra.dal.dataobject.message.UserAnnouncement;
import com.cmict.data.infra.controller.admin.message.vo.UserAnnouncementQO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户公告表
 *
 * @author hccake 2020-12-25 08:04:53
 */
@Mapper
public interface UserAnnouncementMapper extends BaseMapperX<UserAnnouncement> {

    /**
     * 分页查询
     *
     * @param qo 查询对象
     * @return 分页结果数据 PageResult
     */
    default PageResult<UserAnnouncement> queryPage(UserAnnouncementQO qo) {
        return selectPage(qo, new LambdaQueryWrapperX<UserAnnouncement>()
                .eqIfPresent(UserAnnouncement::getAnnouncementId, qo.getAnnouncementId())
                .eqIfPresent(UserAnnouncement::getUserId, qo.getUserId())
                .likeIfPresent(UserAnnouncement::getUsername, qo.getUsername())
                .in(qo.getIdList() !=null && qo.getIdList().size()>0, UserAnnouncement::getAnnouncementId, qo.getIdList())
                .eq(UserAnnouncement::getSendDeleted, false)
                .orderByDesc(UserAnnouncement::getId));
    }

    /**
     * 分页查询
     *
     * @param qo 查询对象
     * @return 分页结果数据 PageResult
     */
    default PageResult<UserAnnouncement> queryTemplatePage(UserAnnouncementQO qo) {
        return selectPage(qo, new LambdaQueryWrapperX<UserAnnouncement>()
                .eqIfPresent(UserAnnouncement::getAnnouncementId, qo.getAnnouncementId())
                .eqIfPresent(UserAnnouncement::getUserId, qo.getUserId())
                .likeIfPresent(UserAnnouncement::getUsername, qo.getUsername())
                .in(UserAnnouncement::getAnnouncementId, qo.getIdList())
                .eq(UserAnnouncement::getSendDeleted, false)
                .orderByDesc(UserAnnouncement::getId));
    }

    /**
     * 更新用户公共信息至已读状态
     *
     * @param userId         用户ID
     * @param announcementId 公告ID
     */
    default void updateToReadState(Long userId, List<Long> announcementId) {
        if (CollectionUtils.isNotEmpty(announcementId)) {
            //不为空
            LambdaUpdateWrapper<UserAnnouncement> wrapper = Wrappers.<UserAnnouncement>lambdaUpdate()
                    .set(UserAnnouncement::getState, UserAnnouncementStateEnum.READ.getValue())
                    .set(UserAnnouncement::getReadTime, LocalDateTime.now())
                    .in(UserAnnouncement::getAnnouncementId, announcementId)
                    .eq(UserAnnouncement::getUserId, userId);
            this.update(null, wrapper);
        } else {
            //为空全部设置为已读
            LambdaUpdateWrapper<UserAnnouncement> wrapper = Wrappers.<UserAnnouncement>lambdaUpdate()
                    .set(UserAnnouncement::getState, UserAnnouncementStateEnum.READ.getValue())
                    .set(UserAnnouncement::getReadTime, LocalDateTime.now())
                    .eq(UserAnnouncement::getUserId, userId);
            this.update(null, wrapper);
        }

    }

    default List<UserAnnouncement> selectListByUserIds(UserAnnouncementQO qo) {
        return selectList(new LambdaQueryWrapperX<UserAnnouncement>().eq(UserAnnouncement::getUserId, qo.getUserId())
                .in( qo.getStateList()!=null && qo.getStateList().size()>0 ,UserAnnouncement::getState, qo.getStateList())
                .eq(UserAnnouncement::getRecDeleted, false));
    }

    default boolean updateByIds(UserAnnouncement userAnnouncement, List<Long> ids) {
        QueryWrapper<UserAnnouncement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(UserAnnouncement::getId, ids);
        int update = this.update(userAnnouncement, queryWrapper);
        return SqlHelper.retBool(update);
    }

    default  List<UserAnnouncement> selecRemindListByUserIds(UserAnnouncementQO qo){
        return selectList(new LambdaQueryWrapperX<UserAnnouncement>().eq(UserAnnouncement::getUserId, qo.getUserId())
                .eq(UserAnnouncement::getState, qo.getState())
                .eq(UserAnnouncement::getRecDeleted, false));
    }
    @Select("${sql}")
    List<Long> getIdListByTemplate(String sql);
    @Select("${sql}")
    List<Long> updateTenantIdByAnnouncementId(String sql);

    @Select("DELETE FROM `notify_user_announcement` WHERE announcement_id = #{announcementId}")
    void deleteByAnnouncementId(Long announcementId);

    @Select("${sql}")
    List<AnnouncementDTO> getAnnouncementsAndNotices(String sql);
}